<?php
/**
 * 商品管理
 * @author 齐迹  email:smpss2012@gmail.com
 *
 */
class c_goods extends base_c {
	function __construct($inPath) {
		parent::__construct ();
		if (self::isLogin () === false) {
			$this->ShowMsg ( "请先登录！", $this->createUrl ( "/main/index" ) );
		}
		if (self::checkRights ( $inPath ) === false) {
			//$this->ShowMsg("您无权操作！",$this->createUrl("/system/index"));
		}
		$this->params['inpath'] = $inPath;
		$this->params ['head_title'] = "商品管理-" . $this->params ['head_title'];
	}
	
	private function dealPicture($picture) {
		if(!$picture) {
			return "";
		}
	//	echo "FUCK........................................................".$picture;
		$image_fn = md5($picture);
	//	echo $image_fn;
		$thumb_fn = $image_fn."thumb.jpg";
		$image_fn = $image_fn.".jpg";
		/*
		$gid = ( int ) $_COOKIE [$tmp_fn];
		$cacheName = "action_code_group_" . $tmp_fn;
		$cache = SCache::getCacheEngine ( 'file' );
		$cache->init ( array ("dir" => SlightPHP::$appDir . "/cache", "depth" => 3 ) );
		$rs = $cache->get ( $cacheName );
		if(!$rs || !$cache){
			$rs = base64_decode($picture);
			$cache->set ( $cacheName, $rs );
		}
		$url = $cache->_getDir($cacheName, true);
		 */
		$path = SlightPHP::$appDir . "/cache/";
		$shit = 50;
		echo $path.$image_fn;
		if(!file_exists($path.$thumb_fn)) {
			$tmp_content = base64_decode($picture);
		//	echo $tmp_content;
			//create thumb
			$imgObj = imagecreatefromstring($tmp_content);
			$imgW = imagesx($imgObj);
			$imgH = imagesy($imgObj);
			$ratio = $imgW>$imgH ? ($imgW>$shit?$imgW/$shit:1) : ($imgH>$shit?$imgH/$shit:1);
			$thumbW = intval($imgW/$ratio);
			$thumbH = intval($imgH/$ratio);
			$thumbObj = imagecreatetruecolor($thumbW, $thumbH);
			imagecopyresampled($thumbObj, $imgObj, 0, 0, 0, 0, $thumbW, $thumbH, $imgW, $imgH);
			imagejpeg($imgObj, $path.$image_fn);
			imagejpeg($thumbObj, $path.$thumb_fn);
			imagedestroy($imgObj);
			imagedestroy($thumbObj);
		}
		$url = base_Constant::ROOT_DIR . "/app/cache/" ;

		$ret = array();
		$ret['imageurl'] = $url.$image_fn;
		$ret['thumburl'] = $url.$thumb_fn;
		//print_r($ret);
		return $ret;
	}
	
	function pageindex($inPath) {
		$url = $this->getUrlParams ( $inPath );
		$page = $url['page']?(int)$url['page']:1;
		//$categoryObj = new m_category ();
		//$this->params['catelist'] = $categoryObj->getOrderCate('&nbsp;&nbsp;&nbsp;&nbsp;');
		$condi = '';
		$goodsObj = new m_goods();
		if($_POST){
			$key = base_Utils::getStr($_POST['key'],'html');
		//	$cat_id = (int)$_POST['cat_id'];
			$this->params['key'] = $key;
		//	$this->params['cat_id'] = $cat_id;
			$tableName = $goodsObj->tableName();
			if($key){
				$condi .= "{$tableName}.goods_name like '%{$key}%' or {$tableName}.goods_sn like '%{$key}%'";
			}
		//	if($cat_id){
		//		 $condi = $condi ? $condi." and {$tableName}.cat_id = {$cat_id}" : "{$tableName}.cat_id = {$cat_id}";
		//	}
		}
		$rs = $goodsObj->getGoodsList($condi,$page);
		if($rs->items)
		{
			foreach ($rs->items as & $item) {
				$item['goods_pic']=$this->dealPicture($item['goods_pic']);
			}
		}
		$this->params ['goods'] = $rs->items;
		$this->params ['pagebar'] = $this->PageBar ( $rs->totalSize, base_Constant::PAGE_SIZE, $page, $inPath );
		
		return $this->render ( 'goods/index.html', $this->params );
	}
	
	function pageaddgoods($inPath) {
		$url = $this->getUrlParams ( $inPath );
		$goods_id = ( int ) $url ['gid'] > 0 ? ( int ) $url ['gid'] : ( int ) $_POST ['goods_id'];
		$mode=(int)$url['gid']>0?0:1;
		$goodsObj = new m_goods($goods_id);
		if($_POST){
			
			//图片需要在这里encode
			if(isset($_FILES["goods_pic_origin"]) and $_FILES["goods_pic_origin"]["error"]==UPLOAD_ERR_OK) {
		//		echo $_FILES["goods_pic_origin"]["tmp_name"];
				$file_content=file_get_contents($_FILES["goods_pic_origin"]["tmp_name"]);
		//		echo $file_content;
				if($file_content===false)
					$this->ShowMsg ( "FUCK操作失败" . $goodsObj->getError () );
				$file_content=base64_encode($file_content);
			} else {
				$file_content="";
			}
			$_POST['goods_pic'] = $file_content;
			/******************************/
			$post = base_Utils::shtmlspecialchars ( $_POST );
			$res = $goodsObj->create ( $post );
			if ($res) {
			//	base_Utils::ssetcookie(array('cat_id'=>$post['cat_id']));
				$this->ShowMsg ( "操作成功!", $this->createUrl ( "/goods/addgoods" ), 3, 1 );
			} else{
				$this->ShowMsg ( "操作失败: " . $goodsObj->getError ());
				return $this->render ( '/goods/addgoods.html', $this->param);
			}
		}
		//$categoryObj = new m_category ();
	//	$this->params['cat_id'] = (int)$_COOKIE['cat_id'];
	//	$this->params['catelist'] = $categoryObj->getOrderCate('&nbsp;&nbsp;&nbsp;&nbsp;');
		$this->params['goods'] = $goodsObj->selectOne("goods_id={$goods_id}");
	//	$this->params['goods']['goods_pic']= $this->dealPicture($this->params['goods']['goods_pic']);
		if($mode===0)	
			return $this->render ( '/goods/editgoods.html', $this->params );
		else if($mode===1)
			return $this->render ( '/goods/addgoods.html', $this->params );
	}
	
}
